#import "ProgressView.h"

@interface ProgressView ()

@property (nonatomic, strong) CALayer *progress;
@property (nonatomic)         CGFloat current;

@end

@implementation ProgressView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self=[super initWithFrame:frame]) {
        [self setUpLayer];
    }
    return self;
}

- (void)setUpLayer {
    self.layer.backgroundColor = [UIColor blackColor].CGColor;
    _progress = [CALayer layer];
    _color = [UIColor orangeColor];
    _progress.backgroundColor = _color.CGColor;
    _progress.frame = self.bounds;
    self.layer.cornerRadius = 13;
    _progress.cornerRadius = 13;
    _current = CGRectGetWidth(self.bounds);
    [self.layer addSublayer:_progress];
}

- (void)startTimer {
   progressStartTimer = [NSTimer scheduledTimerWithTimeInterval:PROGRESS_SEPARATE_TIME target:self selector:@selector(beginAnimate) userInfo:nil repeats:YES];
}

- (void)beginAnimate {
    if (_current < _decrease) {
        _progress.frame = CGRectMake(CGRectGetMinX(self.bounds), CGRectGetMinY(self.bounds), 0, CGRectGetHeight(self.bounds));
        //移除定时器
        [progressStartTimer invalidate];
    }
    else{
        _current -= _decrease;
        _progress.frame = CGRectMake(CGRectGetMinX(self.bounds), CGRectGetMinY(self.bounds), _current, CGRectGetHeight(self.bounds));

    }
}

@synthesize color = _color;
- (void)setColor:(UIColor *)color {
    
    _color = color;
    _progress.backgroundColor = color.CGColor;
}

- (UIColor *)color {
    
    return _color;
}

@end
